package com.mowh.multithreading.future;

/**
 * @author mowh1gd
 * @create 2023/11/24 10:58
 */
public class FutureData implements Data {
    private RealData realData = null;
    private boolean ready = false;

    public synchronized void setRealData(RealData realData) {
        if (ready) {//非守护条件：数据未生成，直接返回
            return; //balk防止重复赋值操作
        }
        this.realData = realData;//守护条件：数据已生成则传入给FutureData的属性realData
        this.ready = true;
        notifyAll();
    }

    @Override
    public synchronized String getContent() {
        while (!ready) {
            try {
                wait();
            } catch (InterruptedException e) {
            }
        }
        return realData.getContent();//守护条件：赋值给属性realData后才能获取content
    }
}
